<?php

namespace App\HttpController\Api;

use App\Think\Org\Sms as Smslib;
use App\Think\Validate;
use App\Think\Db;

/**
 * 手机短信接口Sms
 */
class Sms extends ApiBase
{
    protected $noNeedLogin = ['*'];

    /**
     * 发送验证码
     * ApiMethod (POST)
     * @param string $mobile 手机号
     * @param string $event 事件名称:register=注册,mobilelogin=手机登录,changemobile=修改手机,changepwd=修改密码,resetpwd=重置密码,bank_bind=绑定银行卡,agent_apply=代理申请
     * @param string $captcha 图形验证码|event=resetpwd时,必选
     * @param string $captcha_id 图形验证码ID
     */
    public function send()
    {
        $mobile     = $this->input("mobile");
        $event      = $this->input("event");
        $captcha    = $this->input("captcha");
        $captcha_id = $this->input("captcha_id");

        $event = $event ? $event : 'register';
        if (!$mobile || !Validate::regex($mobile, "^1\d{10}$")) {
            return $this->error(__('手机号不正确'));
        }
        if(in_array($event, ['resetpwd'])){
            $cls = new \App\Think\Captcha();
            if(!$cls->captcha_check($captcha, $captcha_id)) return $this->error(__('图形验证码不正确'));
            $user = db_get_row('user', "mobile='{$mobile}'");
            if (!$user) return $this->error(__('当前手机号未绑定账号'));
        }

        $last = Smslib::get($mobile, $event);
        if ($last && time() - $last['createtime'] < 60) return $this->error(__('发送频繁,请稍后再试')); //每分钟1次
        // $ipSendTotal = Db::name('sms')->where("ip='".$this->clientRealIP()."' AND createtime>='".strtotime('-1 hours')."'")->count();
        // if ($ipSendTotal >= 20) return $this->error(__('发送频繁,请稍后再试')); //每个ip1小时只能20次

        if ($event) {
            $userinfo = db_get_row('user', "mobile='{$mobile}'");
            if ($event == 'register' && $userinfo) {
                //已被注册
                return $this->error(__('当前号码已被注册'));
            } elseif (in_array($event, ['changemobile']) && $userinfo) {
                //被占用
                return $this->error(__('当前号码已被注册'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) ) {

                //未注册
                if(!$userinfo) return $this->error(__('未注册,非法操作'));
            }
        }
        // if (!Hook::get('sms_send')) return $this->error(__('请在后台插件管理安装短信验证插件'));
        $ret = Smslib::send($mobile, null, $event, $this->clientRealIP());
        if ($ret) {
            return $this->success(__('发送成功'));
        } else {
            return $this->error(__('发送失败，请检查短信配置'));
        }
    }

    /**
     * 检测验证码
     * @param string $mobile 手机号
     * @param string $event 事件名称
     * @param string $captcha 验证码
     */
    public function check()
    {
        $mobile = $this->input("mobile");
        $event = $this->input("event");
        $event = $event ? $event : 'register';
        $captcha = $this->input("captcha");

        if (!$mobile || !Validate::regex($mobile, "^1\d{10}$")) {
            return $this->error(__('手机号不正确'));
        }
        if ($event) {
            $userinfo = db_get_row('user', "mobile='{$mobile}'");
            if ($event == 'register' && $userinfo) {
                //已被注册
                return $this->error(__('已被注册'));
            } elseif (in_array($event, ['changemobile']) && $userinfo) {
                //被占用
                return $this->error(__('已被占用'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                return $this->error(__('未注册'));
            }
        }
        $ret = Smslib::check($mobile, $captcha, $event);
        if ($ret) {
            return $this->success(__('success'));
        } else {
            return $this->error(__('验证码不正确'));
        }
    }
}
